package cn.jobstart.controller;

import cn.jobstart.searchExt.AdminSearchExt;
import cn.jobstart.service.IAdminPermissionGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.jobstart.web.common.SkJsonResult;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RestController;
import cn.jobstart.pubInter.exception.SkException;
import cn.jobstart.pubInter.IBaseService;
import cn.jobstart.dto.AdminDTO;
import cn.jobstart.dto.AdminPageDTO;
import cn.jobstart.modelExt.AdminExt;
import cn.jobstart.service.IAdminService;
import cn.jobstart.vo.AdminDetailVO;
import cn.jobstart.vo.AdminListVO;
import cn.jobstart.vo.AdminDomainVO;
import cn.jobstart.vo.AdminDetailDomainVO;
import cn.jobstart.export.AdminDomainExport;
import org.springframework.validation.annotation.Validated;

import java.util.Set;
import java.util.HashSet;
import java.util.List;

import cn.jobstart.common.IdGen;
import cn.jobstart.common.StringUtil;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;

import cn.jobstart.web.common.SecurityUtil;
import org.springframework.web.bind.annotation.PostMapping;
import cn.jobstart.exception.ExceptionMessage;
import cn.jobstart.pubInter.exception.BusinessException;
import cn.jobstart.vo.RightsDetailDomainMiniVO;


/**
 * @author sunke
 * @Date 2023-09-06 15:00:20
 * @description AdminController      系统用户
 */
@Api(tags = "人才管理系统-系统用户相关", description = "系统用户相关接口")
@RestController(value = "adminRestfulController")
@RequestMapping("/admin/")
@Validated
public class AdminController extends ApiManageController<AdminDTO, AdminPageDTO, AdminExt, AdminSearchExt, AdminDomainVO, AdminDetailDomainVO, AdminDetailVO, AdminListVO, String> {

    @Resource(name = "adminService")
    private IAdminService adminService;

	@Resource
	private IAdminPermissionGroupService adminPermissionGroupService;


    @Override
    public IBaseService<AdminExt, AdminSearchExt, String> getBaseService() throws SkException {
        return adminService;
    }


    @Override
    public boolean beforePostAdd(AdminDTO dto, AdminExt domain, HttpServletRequest request, HttpSession session) throws SkException {
        String username = domain.getUsername();
        domain.setId(IdGen.uuid());
        domain.setPassword(SecurityUtil.generateUserPasswordByPswAndSalt(domain.getPassword(), domain.getId()));

        if (StringUtil.isNotObjEmpty(adminService.getInfoByUsername(username))) {
            throw new BusinessException(ExceptionMessage.EXIST_CLIENT);
        }


        return super.beforePostAdd(dto, domain, request, session);
    }

    @ApiOperation(value = "我的权限", notes = "我的权限接口", httpMethod = "POST")
    @PostMapping("myRights")
    public SkJsonResult<List<RightsDetailDomainMiniVO>> myRights(HttpServletRequest request) throws SkException {
        return SkJsonResult.ok().setData(mapList(adminService.getAllRightsListByStaffId(getJwtsUtil().getAdminId(request), true), RightsDetailDomainMiniVO.class));
    }


    @Override
    public String getBaseRoute() {
        return "/admin/";
    }

    /**
     * 当前模板名称
     *
     * @return
     */
    @Override
    public String getModelName() {
        return "系统用户";
    }

    @Override
    public Class exportExcelClass() {
        return AdminDomainExport.class;
    }

    @Override
    public Set<String> totalMapSet() {
        Set totalMapSet = new HashSet();
        totalMapSet.add("count(admin.a_id) as countNum");
        return totalMapSet;
    }


}